package com.future.controller;

import java.util.Date;
import java.util.List;

import com.future.common.annotation.ControllerMapping;
import com.future.common.annotation.JsonBody;
import com.future.common.base.BaseController;
import com.future.common.exception.AppException;
import com.future.common.global.PlatFormContext;
import com.future.common.utils.IDUtil;
import com.future.dao.BookedDao;
import com.future.model.Booked;
import com.future.requestbean.FreeClassRoomQueryBean;
import com.future.requestbean.NewBookBean;
import com.future.service.BookedService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * 教室预定
 * 
 * @author ThinkPad
 *
 */
@ControllerMapping("/booked")
public class BookedController extends BaseController {
	@Inject
	private BookedService bookedService;
	@Inject
	private BookedDao bookedDao;

	// 获取所有教室的信息
	public void getAllClassRoomInfo() {
		renderResult("classroominfo", this.bookedService.getAllClassRoomInfo());
	}

	// 获取我的预定信息
	public void getMyBookedClassRoom() {
		renderResult("mybookedclassroom", this.bookedService.getMyBookedClassRoom());
	}

	// 获取空闲教室
	public void getFreeClassRoom(@JsonBody FreeClassRoomQueryBean freeClassRoomQueryBean) {
		
		if (freeClassRoomQueryBean.getStartDate().compareTo(freeClassRoomQueryBean.getEndDate()) >= 0) {
			throw new AppException("终止时间必须大于起始时间!");
		}
		
		if (freeClassRoomQueryBean.getStartDate().compareTo(new Date()) < 0) {
			throw new AppException("请选择当前时间之后的时间作为开始时间!");
		}
		
		renderResult("classroominfo", this.bookedService.getFreeClassRoom(freeClassRoomQueryBean));
	}

	// 教室预定
	@Before(Tx.class)
	public void book(@JsonBody NewBookBean newBookBean) {
		List<Booked> bookedRoomList = this.bookedDao.getBookedRoom(newBookBean.getClassroomno(),
				newBookBean.getStartdate(), newBookBean.getEnddate());
		if (bookedRoomList != null && bookedRoomList.size() > 0) {
			throw new AppException("该教室在本时间段内已经被预定了，请选择其他的进行预定");
		}
		if (newBookBean.getStartdate().compareTo(newBookBean.getEnddate()) >= 0) {
			throw new AppException("终止时间必须大于起始时间!");
		}
		
		if (newBookBean.getStartdate().compareTo(new Date()) < 0) {
			throw new AppException("开始时间请选择当前时间之后!");
		}
		Booked booked = new Booked();
		booked.setBookedid(IDUtil.getId());
		booked.setUserid(PlatFormContext.getCurrentUser().getUserid());
		booked.setName(PlatFormContext.getCurrentUser().getName());
		booked.setXueyuan(PlatFormContext.getCurrentUser().getXueyuan());
		booked.setClassroomno(newBookBean.getClassroomno());
		booked.setStartdate(newBookBean.getStartdate());
		booked.setEnddate(newBookBean.getEnddate());
		booked.setNoticed("0");
		booked.save();
		renderSuccess();
	}
	/**
	 * 获取我的预定快要到时间的教室
	 */
	public void getCloseToEndBooking(){
		renderResult("classroom", this.bookedDao.getCloseToEndBooking(PlatFormContext.getCurrentUser().getUserid()));
	}
	
	/**
	 * 关闭特定教室到期提醒
	 * @param bookedid
	 */
	public void closeNotice(String bookedid){
		this.bookedDao.findById(bookedid).setNoticed("1").update();
		renderSuccess();
	}
}
